package service;

import dao.UserDao;
import daoImpl.UserDaoImpl;
import util.MD5Encryption;
import vo.Employee;

import java.io.UnsupportedEncodingException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

public class UserService {

    UserDaoImpl ud = new UserDaoImpl();

    public Map<String,Object> login(String username,String password){

        Map<String,Object> empMap = null;

        try {
            password = MD5Encryption.getEncryption(password);
            empMap = ud.selectEmpByUsernameAndPassword(username, password);

            System.out.println("=============================="+empMap);

            //获取到登录数据之后，年龄字段需要单独处理
            if(null!=empMap && empMap.size()>0){

                //获取当前时间的年份
                int nowYear = LocalDate.now().getYear();

                //获取当前员工的生日
                String birth = (String) empMap.get("BIRTH");

                System.out.println("birth"+birth);
                //从生日中获取年份
                int birthYear = LocalDate.parse(birth,DateTimeFormatter.ofPattern("yyyy-MM-dd")).getYear();

                empMap.put("age",nowYear-birthYear);

            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        return empMap;

    }

}
